/**
 * 
 */
package com.zc.BankCreditManagement.Servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.zc.BankCreditManagement.JavaBean.Customer;
import com.zc.BankCreditManagement.JavaBean.Manager;
import com.zc.BankCreditManagement.ServiceImpl.CustomerServiceImpl;
import com.zc.BankCreditManagement.ServiceImpl.ManagerServiceImpl;
import com.zc.BankCreditManagement.ServiceImpl.UserServiceImpl;

/**
 * @author ZhangCheng
 * @time 2019年9月20日
 */
@WebServlet("/UserInfoManagementServlet")
public class UserInfoManagementServlet extends HttpServlet {

	/**
	 * 一个特定标识符的序列号
	 */
	private static final long serialVersionUID = -5280846820052808751L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String type = req.getParameter("Type");
		System.out.println(type);
		if ("Customer".equals(type)) {
			CustomerInfo(req, resp);
		} else if ("ChangePWD".equals(type)) {
			cusChangePWD(req, resp);
		} else if ("Manager".equals(type)) {
			ManagerInfo(req, resp);
		} else if ("ManChangeProfile".equals(type)) {
			ManChangeProfile(req, resp);
		}else if("CusChangeProfile".equals(type)) {
			CusChangeProfile(req,resp);
		}
	}
	/**
	 * 	根据请求修改客户资料
	 * 
	 */
	private void CusChangeProfile(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		int cusId = Integer.parseInt(req.getParameter("peoId"));

		String cusName = req.getParameter("cusName");
		String corpoRepresentative = req.getParameter("corpoRepresentative");
		String regiAddress = req.getParameter("regiAddress");
		String email = req.getParameter("Email");
		
		Customer cus = new Customer(cusId, cusName, corpoRepresentative, regiAddress,email );
		CustomerServiceImpl cSI = new CustomerServiceImpl();
		
		if (cSI.updateCustomer(cus, true)== 1) {
			resp.getWriter().write("Yes");
		} else {
			resp.getWriter().write("null");
		}
	}
	/**
	 * 	根据请求修改经理资料
	 * 
	 */
	private void ManChangeProfile(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		int manId = Integer.parseInt(req.getParameter("peoId"));
		String manName = req.getParameter("manName");
		String manWorkPhone = req.getParameter("manWorkPhone");
		int manLevel = Integer.parseInt(req.getParameter("manLevel"));
		String email = req.getParameter("email");
		Manager man = new Manager(manId, manName, manLevel, manWorkPhone,email);
		ManagerServiceImpl mSI = new ManagerServiceImpl();
		
		if (mSI.updateManager(man, true) == 1) {
			resp.getWriter().write("Yes");
		} else {
			resp.getWriter().write("null");
		}
	}
	/**
	 * 	根据请求修改密码
	 * 
	 */
	private void cusChangePWD(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String userId = req.getParameter("peoId");
		String newPwd = req.getParameter("PWD");
		
		UserServiceImpl uSI = new UserServiceImpl();
		
		if (uSI.resetPwdWithNewPwd(userId, newPwd) == 1) {
			resp.getWriter().write("Yes");
		} else {
			resp.getWriter().write("null");
		};
	}
	/**
	 * 	根据请求获取经理个人信息
	 * 
	 */
	private void ManagerInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		ManagerServiceImpl mSI = new ManagerServiceImpl();
		Manager m = mSI.getThisInfoById(Integer.parseInt(req.getParameter("peoId")), true);

		Gson gson = new Gson();
		String msg = gson.toJson(m);
		resp.setContentType("json/application;charset=UTF-8");
		resp.getWriter().write(msg);
	}
	/**
	 * 	根据请求获取客户个人信息
	 * 
	 */
	private void CustomerInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		CustomerServiceImpl cSI = new CustomerServiceImpl();
		Customer c = cSI.getThisInfoById(Integer.parseInt(req.getParameter("peoId")), true);

		Gson gson = new Gson();
		String msg = gson.toJson(c);
		resp.setContentType("json/application;charset=UTF-8");
		resp.getWriter().write(msg);
	}
}
